import unittest
from Autotests.BaseTest import AirClientTestCase
from Autotests.LocalMachine.Pages.AirClient.OptionsPage.ScriptTab import ScriptsTab


class OptionsScriptsTest(AirClientTestCase):
    def __init__(self, *args, **kwargs):
        super(OptionsScriptsTest, self).__init__(*args, **kwargs)
        self._scriptTab = ScriptsTab()

    def test_OptionsAddScript(self):
        self._scriptTab.goToHomeLocation()
        scriptName = 'ScriptName'
        userName = 'UserName'
        password = 'Password'
        timeout = 0
        errorMessage = 'ErrorMessage'

        scriptCount = self._scriptTab.getScriptCount()
        self._scriptTab.addScript(scriptName, userName, password, timeout, errorMessage, True)
        self.assertEqual(self._scriptTab.getScriptCount(), scriptCount + 1,
                         "Scripts count hasn't been incremented after adding script")

    def test_OptionsDeleteScript(self):
        self._scriptTab.goToHomeLocation()
        scriptForDelete = "ScriptForDelete"
        self._scriptTab.deleteScript(scriptForDelete)
        self._scriptTab.addScript(scriptForDelete)

        self._scriptTab.deleteScript(scriptForDelete)
        self.timeoutAssertTrue(lambda: not self._scriptTab.hasScript(scriptForDelete),
                               "Script hasn't been removed")

    def test_OptionsEditScript(self):
        self._scriptTab.goToHomeLocation()
        scriptForEditName = "ScriptForEdit"
        newScriptName = 'newScriptName'
        newUserName = 'newUserName'
        newPassword = 'newPassword'
        newTimeout = '1'
        newErrorMessage = 'newErrorMessage'
        failOnError = True
        infoTuple = (newScriptName, newUserName, newPassword, newTimeout, newErrorMessage, failOnError)

        self._scriptTab.deleteScript(scriptForEditName)
        self._scriptTab.deleteScript(newScriptName)

        self._scriptTab.addScript(scriptForEditName)
        scriptCount = self._scriptTab.getScriptCount()

        self._scriptTab.modifyScript(scriptForEditName, *infoTuple)

        self.assertEqual(scriptCount, self._scriptTab.getScriptCount(),
                         "Scripts count has been changed after modifying")
        self.assertEqual(infoTuple, self._scriptTab.getScriptInfo(newScriptName),
                         "New script attributes hasn't been applied after modifying")

if __name__ == "__main__":
    unittest.main()